package com.example.contadorcomida;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.IntentService;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class MyService extends IntentService{
	private static final int CONNECTION_TIMEOUT = 0;
	
	public MyService() {
		super("acercaDe");
	}

	public String coneccionDeApertura () throws IOException{
		URL connURL = new URL("http://10.1.2.105/android/acerca.de"); 
		String concatenacionString = ""; 
		HttpURLConnection urlConnection = (HttpURLConnection) connURL.openConnection(); 
		
		try{
			urlConnection.setConnectTimeout(CONNECTION_TIMEOUT); 
			urlConnection.setReadTimeout(CONNECTION_TIMEOUT); 
			
			BufferedReader reader = new BufferedReader( new InputStreamReader(urlConnection.getInputStream()) ); 
			String line = reader.readLine(); 
			
			while(line != null)
			{
				concatenacionString = concatenacionString + line; 
				line = reader.readLine(); 
			}
			return concatenacionString; 
		}finally{
			urlConnection.disconnect(); 
		}
	}

	@Override
	protected void onHandleIntent(Intent arg0) {
		try {
			System.out.println("SYSOUT"); 
			String mensaje = this.coneccionDeApertura();
			
			SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
			Editor editor = preferences.edit();
			editor.putString("acercaDe", mensaje);
			editor.commit();
			
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
}
